<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <div th:replace="admin/head :: head"></div>

    <title>优惠券表单</title>
</head>
<body>
<div class="x-body layui-anim layui-anim-up">
    <form class="layui-form">
        <input type="hidden" name="id" th:value="${discountcoupon?.id}"/>

        <div class="layui-form-item">
            <label for="L_price" class="layui-form-label">
                价格
            </label>
            <div class="layui-input-inline">
                <select class="layui-select" name="price" id="L_price" required="">
                    <option value="">请选择</option>
                    <option th:each="item, index:${prices}" th:value="${item}" th:text="${item}" th:selected="${discountcoupon ne null and discountcoupon.price eq item}"></option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_owner" class="layui-form-label">
                所有人
            </label>
            <div class="layui-input-inline">
                <select class="layui-select" name="owner" id="L_owner" lay-search="" required="">
                    <option value="">请选择</option>
                    <option th:each="item, index:${users}" th:value="${item.id}" th:text="${item.nickname}" th:selected="${discountcoupon ne null and discountcoupon.owner eq item.id}"></option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_originalOwner" class="layui-form-label">
                历史所有人
            </label>
            <div class="layui-input-inline">
                <select class="layui-select" name="originalOwner" id="L_originalOwner" lay-search="">
                    <option value="">请选择</option>
                    <option th:each="item, index:${users}" th:value="${item.id}" th:text="${item.nickname}" th:selected="${discountcoupon ne null and discountcoupon.originalOwner eq item.id}"></option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_expiration" class="layui-form-label">
                过期时间
            </label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="L_expiration" th:value="${#dates.format(discountcoupon.expirationDate, 'yyyy-MM-dd')}" placeholder="yyyy-MM-dd" required="">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <button id="submit-btn" class="layui-btn" lay-filter="add" lay-submit="">
                保存
            </button>
        </div>
    </form>
</div>

<script>
    layui.use(['form', 'layer', 'laydate'], function(){
        $ = layui.jquery;
        var form = layui.form,
            laydate = layui.laydate;

        // 过期时间
        laydate.render({
            elem: '#L_expiration'
        });

        // 自定义验证规则
        form.verify({

        });

        // 监听提交
        form.on('submit(add)', function(data){
            $.ajax({
                type: 'post',
                url: '/admin/discountcoupon/save',
                async: false,
                data: formData.field,
                dataType: 'json',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader($("meta[name='_csrf_header']").attr("content"), $("meta[name='_csrf']").attr("content"));
                },
                success: function (result) {
                    if (result.status == 200) {
                        layer.alert(Messages.SUCCESS, {icon: 6}, function () {
                            // get frame index
                            var index = parent.layer.getFrameIndex(window.name);
                            // refresh parent list and close current dialog
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    } else {
                        layer.alert(result.error == null || result.error == undefined ? Messages.ERROR : result.error, {icon: 1});
                        return false;
                    }
                },
                error: function (e) {
                    layer.alert(Messages.ERROR, {icon: 1});
                }
            })
            return false;
        });
    });
</script>

</body>
</html>